<template>
	<view class="page">
		<view class="top-header-box">
			<view class="search-box">
				<text class="wlIcon-sousuo1 text-bold" style="color: #a4a4a4;font-size: 30rpx;"></text>
				<picker class="picker-map-box" mode="region" @change="cityChange">
					<view class="picker-map">
						<text>{{ city }}</text>
						<u-icon name="arrow-down" size="20" style="margin-left: 5rpx;"></u-icon>
					</view>
				</picker>
				<view class="search-input">
					<input type="text" v-model="shop_name" confirm-type="search" @confirm="searchLis" placeholder="请输入小区名称">
				</view>
			</view>
		</view>
		
		<view class="show-box-btm">
			<view class="show-user-map" v-if="userShop">
				<view class="user-head-img"><image :src="$wanlshop.oss(userShop.avatar)" mode=""></image></view>
				<view class="user-head-name">店长： {{ userShop.nickname }}</view>
				<view class="user-data-map">
					<view class="user-map-title">{{ userShop.shopname }}</view>
					<view class="user-map-detail">{{ userShop.city + userShop.address}}</view>
				</view>
			</view>
			
			<!-- 用户暂未绑定店铺 -->
			<view class="show-user-map flex align-center justify-center" v-if="!userShop">
				<view class="user-map-title">暂未绑定店铺</view>
			</view>
			
			<view class="nearby-house">
				<view class="nearby-title">
					附近小区
				</view>
				
				<view class="house-content">
					<view class="item-nearby-house" v-for="item in list" :key="item.id" v-if="item.length != 0" @click="getBindShop(item.id)">
						<view class="flex w-100">
							<view class="left-house-head-img"><image :src="$wanlshop.oss(item.avatar)" mode=""></image></view>
							<view class="right-house-content w-100 flex flex-column">
								<view class="label-house-one flex align-center justify-between">
									<view class="label-house-name">{{ item.shopname }}</view>
									<view class="label-house-jl" v-if="item.distance">距离{{ item.distance }}</view>
								</view>
								<view class="label-house-user-name">
									店长： <text>{{ item.user.nickname }}</text>
								</view>
								<view class="lable-house-user-map-detial">
									{{ item.city + item.shop_config.address }}
								</view>
							</view>
						</view>
						<view class="right-house"><u-icon name="arrow-right" size="35" color="#a8a8a8" style="margin-left: 25rpx;"></u-icon></view>
					</view>
					
					<!-- 暂无店铺 -->
					<view class="flex align-center justify-center" v-if="list.length == 0">
						<text>暂无店铺</text>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import { mapState, mapMutations } from 'vuex';
	export default {
		data() {
			return {
				city: '', //	string	否	地区名称
				shop_name: '', //	string	否	店铺名称
				longitude: '', //	string	是	经度
				latitude: '', //	string	是	纬度
				page: 1, //	string	否	页数 默认1
				per_page: 5, //	string	否	每页条数 默认15条
				
				userShop: null, // 用户绑定的店铺
				list: [], //店铺列表
				total: 0, // 店铺总数
				
				orderType: false, // 是否从订单页来切换店铺
			}
		},
		onLoad(option) {
			uni.showLoading()
			if(option.order) this.orderType = true;
			this.getLocation();
			this.getUserShopDetail();
		},
		computed: {
			...mapState(['user'])
		},
		watch: {
			shop_name(newVal) {
				if(newVal == '') {
					this.getSelfShop()
				}
			}
		},
		methods: {
			
			getUserShopDetail() {
				//判断用户是否绑定店铺
				let { shop_id } = this.user;
				if(shop_id == 0) return
				
				// 获取店铺详情
				let self = this;
				this.$api.post({
					url: '/wanlshop.shop/offlineShop',
					data: {id: shop_id},
					success(res) {
						self.userShop = res
					}
				})
			},
			
			//切换城市
			cityChange(e) {
				let { value } = e.detail;
				this.city = value[2];
				this.getSelfShop()
			},
			
			getLocation() {
				const self = this;
				uni.getLocation({
					success(res) {
						let { longitude, latitude} = res;
						self.longitude = longitude;
						self.latitude = latitude
						uni.request({
							url: 'https://restapi.amap.com/v3/geocode/regeo',
							data: {
								key: self.$wanlshop.config('amapkey'),
								location: `${longitude},${latitude}`
							},
							success(res2) {
								if(res2.statusCode == 200 && res2.data.status == 1) {
									let { regeocode : { addressComponent } } = res2.data
									self.city = addressComponent.district;
									self.getSelfShop();
								}
							}
						})
					}
				})
			},
			
			// 搜索店铺
			searchLis() {
				let { shop_name } = this;
				if(shop_name == '') return this.$wanlshop.msg('请输入要搜索的店铺');
				else this.getSelfShop()
			},
			
			getSelfShop() {
				const self = this;
				let { city, shop_name, longitude, latitude, page, per_page } = this;
				let data = { city, shop_name, longitude, latitude, page, per_page };
				
				this.$api.post({
					url: '/wanlshop.shop/lis',
					data,
					success(res) {
						uni.hideLoading()
						self.list = res.data;
						self.total = res.total;
					}
				})
			},
			
			//绑定店铺
			getBindShop(id) {
				let self = this;
				let data = new Object();
				data.shop_id = id;
				
				//判断是否切换订单店铺
				if(this.orderType) {
					let pages = getCurrentPages();
					let page = pages[pages.length - 2];
					page.$vm.userShop_id = id;
					uni.navigateBack()
					return
				}
				
				this.$api.post({
					url: '/wanlshop.user/bindShop',
					data,
					success(res) {
						uni.showToast({title:'绑定成功'});
						self.$store.commit('user/setUserInfo', {shop_id: id});
						setTimeout(() => {
							uni.switchTab({
								url: "/pages/tabBar/index/index"
							})
						},1200)
					}
				})
			}
		}
	}
</script>

<style scoped>
.page {
	position: absolute;
	left: 0;
	top: 0;
	right: 0;
	bottom: 0;
	width: 100%;
	height: 100%;
	background: #FFFFFF !important;
	overflow-y: scroll;
}
.flex { display: flex;}
.align-center { align-items: center;}
.justify-between { justify-content: space-between;}
.flex-column { flex-direction: column;}
.w-100 { width: 100%;}

.top-header-box {
	width: 100%;
	height: 300rpx;
	background-color: #e33011;
	padding: 30rpx 40rpx;
	box-sizing: border-box;
}
.search-box {
	width: 100%;
	height: 65rpx;
	background-color: white;
	border-radius: 40rpx;
	display: flex;
	align-items: center;
	padding: 0 25rpx;
	box-sizing: border-box;
}
.picker-map-box {
	display: flex;
	align-items: center;
	font-size: 28rpx;
	font-weight: bold;
	color: #272727;
	margin-left: 20rpx;
	flex-shrink: 0;
}
.picker-map {
	display: flex;
	align-items: center;
}
.search-input {
	height: 100%;
	margin-left: 20rpx;
	flex-shrink: 0;
}
.search-input input {
	width: 100%;
	height: 100%;
	font-size: 28rpx;
}
.show-box-btm {
	width: 100%;
	position:relative;
	left: 0;
	height: 100rpx;
	top: -70rpx;
}
.show-user-map {
	margin: 0 30rpx;
	height: 370rpx;
	padding: 15rpx;
	box-sizing: border-box;
	border-radius: 18rpx;
	background-image: url();
	background-size: 100% 100%;
	background-repeat: no-repeat;
	position: relative;
	box-shadow: #f4f4f4 0px 5rpx 5rpx; 
}
.user-head-img {
	width: 120rpx;
	height: 120rpx;
	border: solid 1rpx #eeeeee;
	border-radius: 50%;
	overflow: hidden;
	position: absolute;
	left: 50%;
	transform: translate(-50%, -50%);
	background: white;
}
.user-head-img image {
	width: 100%;
	height: 100%;
}
.user-head-name {
	margin-top: 90rpx;
	font-size: 28rpx;
	color: #979797;
	text-align: center;
}
.user-data-map {
	padding: 0 30rpx;
	margin-top: 30rpx;
	text-align: center;
	color: #333333;
}
.user-map-title {
	font-size: 40rpx;
	font-weight: bold;
	line-height: 40rpx;
	margin-bottom: 30rpx;
}
.user-map-detail {
	font-size: 28rpx;
	line-height: 38rpx;
}
.nearby-house {
	margin-top: 50rpx;
}
.nearby-title {
	padding-left: 30rpx;
	border-left: solid 10rpx #e33011;
	font-size: 40rpx;
	line-height: 40rpx;
	font-weight: bold;
}
.house-content {
	display: flex;
	flex-direction: column;
}
.item-nearby-house {
	display: flex;
	align-items: center;
	padding: 50rpx 60rpx 60rpx 30rpx;
	border-bottom: solid 2rpx #f6f6f6;
}
.left-house-head-img {
	width: 80rpx;
	height: 80rpx;
	border-radius: 50%;
	overflow: hidden;
	flex-shrink: 0;
	margin-right: 20rpx;
}
.left-house-head-img image {
	width: 100%;
	height: 100%;
}
.label-house-name {
	font-size: 32rpx;
	font-weight: 800;
	color: #333;
}
.label-house-jl {
	font-size: 28rpx;
	color: #e11d00;
}
.label-house-user-name {
	font-size: 28rpx;
	color: #a7a7a7;
	margin-top: 20rpx;
	margin-bottom: 10rpx;
	line-height: 28rpx;
}
.lable-house-user-map-detial {
	font-size: 28rpx;
	color: #4e4e4e;
	line-height: 35rpx;
}
</style>
